package org.esprit.ocm.client.model.oca.datasource;

import java.util.ArrayList;
import java.util.List;

import org.esprit.ocm.client.model.oca.VmRecord;
import org.esprit.ocm.model.oca.VmOca;

import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.data.fields.DataSourceTextField;
import com.smartgwt.client.widgets.grid.ListGridRecord;

public class VmLocalDS extends DataSource {

	private static VmLocalDS	instance	= null;

	public static VmLocalDS getInstance(List<VmOca> lists) {
//		if (instance == null) {
			instance = new VmLocalDS("vmLocalDS", lists);
//		}
		return instance;
	}

	public VmLocalDS(String id, List<VmOca> lists) {

//		setID(id);

		DataSourceTextField idField = new DataSourceTextField("id", "id");
		DataSourceTextField uid = new DataSourceTextField("uid", "uid");
		DataSourceTextField username = new DataSourceTextField("username", "username");
		DataSourceTextField name = new DataSourceTextField("name", "name");
		DataSourceTextField last_poll = new DataSourceTextField("last_poll", "last_poll");
		DataSourceTextField state = new DataSourceTextField("state", "state");
		DataSourceTextField lcm_state = new DataSourceTextField("lcm_state", "lcm_state");
		DataSourceTextField stime = new DataSourceTextField("stime", "stime");
		DataSourceTextField etime = new DataSourceTextField("etime", "etime");
		DataSourceTextField deploy_id = new DataSourceTextField("deploy_id", "deploy_id");
		DataSourceTextField memory = new DataSourceTextField("memory", "memory");
		DataSourceTextField cpu = new DataSourceTextField("cpu", "cpu");
		DataSourceTextField net_tx = new DataSourceTextField("net_tx", "net_tx");
		DataSourceTextField net_rx = new DataSourceTextField("net_rx", "net_rx");

		DataSourceTextField template_cpu = new DataSourceTextField("template_cpu", "template_cpu");
		DataSourceTextField template_memory = new DataSourceTextField("template_memory", "template_memory");
		DataSourceTextField template_name = new DataSourceTextField("template_name", "template_name");
		DataSourceTextField template_vmid = new DataSourceTextField("template_vmid", "template_vmid");

		DataSourceTextField template_disk_bus = new DataSourceTextField("template_disk_bus", "template_disk_bus");
		DataSourceTextField template_disk_clone = new DataSourceTextField("template_disk_clone", "template_disk_clone");
		DataSourceTextField template_disk_disk_id = new DataSourceTextField("template_disk_disk_id", "template_disk_disk_id");
		DataSourceTextField template_disk_image = new DataSourceTextField("template_disk_image", "template_disk_image");
		DataSourceTextField template_disk_image_id = new DataSourceTextField("template_disk_image_id", "template_disk_image_id");
		DataSourceTextField template_disk_readonly = new DataSourceTextField("template_disk_readonly", "template_disk_readonly");
		DataSourceTextField template_disk_save = new DataSourceTextField("template_disk_save", "template_disk_save");
		DataSourceTextField template_disk_source = new DataSourceTextField("template_disk_source", "template_disk_source");
		DataSourceTextField template_disk_target = new DataSourceTextField("template_disk_target", "template_disk_target");
		DataSourceTextField template_disk_type = new DataSourceTextField("template_disk_type", "template_disk_type");

		DataSourceTextField template_os_arch = new DataSourceTextField("template_os_arch", "template_os_arch");
		DataSourceTextField template_os_boot = new DataSourceTextField("template_os_boot", "template_os_boot");

		DataSourceTextField history_seq = new DataSourceTextField("history_seq", "history_seq");
		DataSourceTextField history_hostname = new DataSourceTextField("history_hostname", "history_hostname");
		DataSourceTextField history_vm_dir = new DataSourceTextField("history_vm_dir", "history_vm_dir");
		DataSourceTextField history_hid = new DataSourceTextField("history_hid", "history_hid");
		DataSourceTextField history_stime = new DataSourceTextField("history_stime", "history_stime");
		DataSourceTextField history_etime = new DataSourceTextField("history_etime", "history_etime");
		DataSourceTextField history_vmmmad = new DataSourceTextField("history_vmmmad", "history_vmmmad");
		DataSourceTextField history_tmmad = new DataSourceTextField("history_tmmad", "history_tmmad");
		DataSourceTextField history_pstime = new DataSourceTextField("history_pstime", "history_pstime");
		DataSourceTextField history_petime = new DataSourceTextField("history_petime", "history_petime");
		DataSourceTextField history_rstime = new DataSourceTextField("history_rstime", "history_rstime");
		DataSourceTextField history_retime = new DataSourceTextField("history_retime", "history_retime");
		DataSourceTextField history_estime = new DataSourceTextField("history_estime", "history_estime");
		DataSourceTextField history_eetime = new DataSourceTextField("history_eetime", "history_eetime");
		DataSourceTextField history_reason = new DataSourceTextField("history_reason", "history_reason");

		setFields(idField, uid, username, name, last_poll, state, lcm_state, stime, etime, deploy_id, memory, cpu, net_tx, net_rx, template_cpu,
				template_memory, template_name, template_vmid, template_disk_bus, template_disk_clone, template_disk_disk_id, template_disk_image,
				template_disk_image_id, template_disk_readonly, template_disk_save, template_disk_source, template_disk_target, template_disk_type,
				template_os_arch, template_os_boot, history_seq, history_hostname, history_vm_dir, history_hid, history_stime, history_etime,
				history_vmmmad, history_tmmad, history_pstime, history_petime, history_rstime, history_retime, history_estime, history_eetime,
				history_reason);

		// setFields(template_cpu, template_memory, template_name,
		// template_vmid);

		// setFields(template_disk_bus, template_disk_clone,
		// template_disk_disk_id, template_disk_image, template_disk_image_id,
		// template_disk_readonly,
		// template_disk_save, template_disk_source, template_disk_target,
		// template_disk_type);

		// setFields(template_os_arch, template_os_boot);

		// setFields(history_seq, history_hostname, history_vm_dir, history_hid,
		// history_stime, history_etime, history_vmmmad, history_tmmad,
		// history_pstime, history_petime, history_rstime, history_retime,
		// history_estime, history_eetime, history_reason);

		List<ListGridRecord> l = new ArrayList<ListGridRecord>();
		for (VmOca from : lists) {

			VmRecord to = new VmRecord();

			to.setId(from.getId());
			to.setUid(from.getUid());
			to.setUsername(from.getUsername());
			to.setName(from.getName());
			to.setLast_poll(from.getLast_poll());
			to.setState(from.getState());
			to.setLcm_state(from.getLcm_state());
			to.setStime(from.getStime());
			to.setEtime(from.getEtime());
			to.setDeploy_id(from.getDeploy_id());
			to.setMemory(from.getMemory());
			to.setCpu(from.getCpu());
			to.setNet_tx(from.getNet_tx());
			to.setNet_rx(from.getNet_rx());

			to.setTemplate_cpu(from.getTemplate_cpu());
			to.setTemplate_memory(from.getTemplate_memory());
			to.setTemplate_name(from.getTemplate_name());
			to.setTemplate_vmid(from.getTemplate_vmid());

			to.setTemplate_disk_bus(from.getTemplate_disk_bus());
			to.setTemplate_disk_clone(from.getTemplate_disk_clone());
			to.setTemplate_disk_disk_id(from.getTemplate_disk_disk_id());
			to.setTemplate_disk_image(from.getTemplate_disk_image());
			to.setTemplate_disk_image_id(from.getTemplate_disk_image_id());
			to.setTemplate_disk_readonly(from.getTemplate_disk_readonly());
			to.setTemplate_disk_save(from.getTemplate_disk_save());
			to.setTemplate_disk_source(from.getTemplate_disk_source());
			to.setTemplate_disk_target(from.getTemplate_disk_target());
			to.setTemplate_disk_type(from.getTemplate_disk_type());

			to.setTemplate_os_arch(from.getTemplate_os_arch());
			to.setTemplate_os_boot(from.getTemplate_os_boot());

			to.setHistory_seq(from.getHistory_seq());
			to.setHistory_hostname(from.getHistory_hostname());
			to.setHistory_vm_dir(from.getHistory_vm_dir());
			to.setHistory_hid(from.getHistory_hid());
			to.setHistory_stime(from.getHistory_stime());
			to.setHistory_etime(from.getHistory_etime());
			to.setHistory_vmmmad(from.getHistory_vmmmad());
			to.setHistory_tmmad(from.getHistory_tmmad());
			to.setHistory_pstime(from.getHistory_pstime());
			to.setHistory_petime(from.getHistory_petime());
			to.setHistory_rstime(from.getHistory_rstime());
			to.setHistory_retime(from.getHistory_retime());
			to.setHistory_estime(from.getHistory_estime());
			to.setHistory_eetime(from.getHistory_eetime());
			to.setHistory_reason(from.getHistory_reason());

			l.add(to);
		}

		setTestData(l.toArray(new VmRecord[0]));
		setClientOnly(true);
	}

}
